<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-row>
        <el-form-item label="元数据连接信息" prop="metadata">
          <el-select
            v-model="queryParams.metadata"
            placeholder="请输入元数据连接信息"
            clearable
            style="width: 300px"
          >
            <el-option
              v-for="dict in dict.type.mysql_jdbc_url"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="服务器IP" prop="serverIp">
          <el-input
            v-model="queryParams.serverIp"
            placeholder="请输入服务器IP"
            clearable
            style="width: 300px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="服务器端口" prop="serverPort">
          <el-input
            v-model="queryParams.serverPort"
            placeholder="请输入服务器端口"
            clearable
            style="width: 300px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="IEC104报文" prop="message">
          <el-input
            v-model="queryParams.message"
            placeholder="请输入IEC104报文"
            type="textarea"
            clearable
            style="width: 800px" rows="5"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-row>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">解析</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="kvList">
      <el-table-column label="场站编号" align="center" key="stationId" prop="stationId" v-if="columns[0].visible" width="200"/>
      <el-table-column label="设备类型" align="center" key="equipmentType" prop="equipmentType" v-if="columns[1].visible"/>
      <el-table-column label="设备ID" align="center" key="equipmentId" prop="equipmentId" v-if="columns[2].visible" width="250"/>
      <el-table-column label="时间戳" align="center" key="timestamp" prop="timestamp" v-if="columns[8].visible" width="200"/>
      <el-table-column label="信息对象地址" align="center" key="infoObjAddr" prop="infoObjAddr" v-if="columns[9].visible"
                       width="300" :show-overflow-tooltip="true"/>
      <el-table-column label="属性名" align="center" key="propertyName" prop="propertyName" v-if="columns[10].visible"/>
      <el-table-column label="备注" align="center" key="remark" prop="remark" v-if="columns[11].visible"/>
      <el-table-column label="值类型" align="center" key="valueKind" prop="valueKind" v-if="columns[12].visible"/>
      <el-table-column label="属性值" align="center" key="propertyValue" prop="propertyValue" v-if="columns[13].visible"/>
      <el-table-column label="上报类型" align="center" key="transferType" prop="transferType" v-if="columns[14].visible"/>
      <el-table-column label="初始属性名" align="center" key="initPropertyName" prop="initPropertyName"
                       v-if="columns[15].visible" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="是否填充" align="center" key="fillData" prop="fillData" v-if="columns[16].visible"/>
      <el-table-column label="标准字段描述" align="center" key="standardDescription" prop="standardDescription"
                       v-if="columns[17].visible" width="200" :show-overflow-tooltip="true"/>
      <el-table-column label="厂商描述" align="center" key="description" prop="description" v-if="columns[18].visible"
                       width="200"
                       :show-overflow-tooltip="true"/>
    </el-table>

  </div>
</template>

<script>
import {parserIec104} from "@/api/system/point_check";

export default {
  name: "Parser",
  dicts: ['sys_normal_disable', 'mysql_jdbc_url'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 角色表格数据
      kvList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        serverIp: undefined,
        serverPort: undefined,
        message: undefined,
        metadata: undefined
      },
      // 表单参数
      form: {},
      // 列信息
      columns: [
        {key: 0, label: `场站ID`, visible: true},
        {key: 1, label: `设备类型`, visible: true},
        {key: 2, label: `设备ID`, visible: true},
        {key: 8, label: `时间戳`, visible: true},
        {key: 9, label: `信息对象地址`, visible: true},
        {key: 10, label: `属性名`, visible: true},
        {key: 11, label: `备注`, visible: true},
        {key: 12, label: `值类型`, visible: true},
        {key: 13, label: `属性值`, visible: true},
        {key: 14, label: `上报类型`, visible: true},
        {key: 15, label: `初始属性值`, visible: true},
        {key: 16, label: `是否填充`, visible: true},
        {key: 17, label: `标准字段描述`, visible: true},
        {key: 18, label: `厂家描述`, visible: true},
      ],
      defaultProps: {
        children: "children",
        label: "label"
      }
    };
  },
  created() {
    this.loading = false;
  },
  methods: {
    /** 查询角色列表 */
    queryData() {
      // 判断条件是否为空
      if (this.queryParams.serverIp === '' || this.queryParams.serverIp === undefined) {
        this.$alert("请输入服务器IP", '系统提示', {
          dangerouslyUseHTMLString: true,
          type: 'success'
        });
        return;
      }
      if (this.queryParams.serverPort === '' || this.queryParams.serverPort === undefined) {
        this.$alert("请输入端口号", '系统提示', {
          dangerouslyUseHTMLString: true,
          type: 'success'
        });
        return;
      }
      if (this.queryParams.message === '' || this.queryParams.message === undefined) {
        this.$alert("请输入IEC104报文", '系统提示', {
          dangerouslyUseHTMLString: true,
          type: 'success'
        });
        return;
      }
      if (this.queryParams.metadata === '' || this.queryParams.metadata === undefined) {
        this.$alert("请选择元数据信息", '系统提示', {
          dangerouslyUseHTMLString: true,
          type: 'success'
        });
        return;
      }

      this.loading = true;
      parserIec104(this.queryParams).then(response => {
          this.kvList = response.data;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    }
  }
};
</script>
